<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Mongo,文件服务," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="1、GridFs文件库使用1.1、添加文件操作GridFs文件库的命令在MONGO_HOMEj/bin 下的mongofiles文件中。我们可以调用该文件中的命令来操作GridFs文件库mongofiles -d gridfs put demo.png 只要往数据库中插入了一个文件，Mongo会为该库新建两个Collection来保存文件相关的内容。这两个Collection的用途我们待会会说道。">
<meta name="keywords" content="Mongo,文件服务">
<meta property="og:type" content="article">
<meta property="og:title" content="Mongo GridFs文件服务">
<meta property="og:url" content="https://gitee.com/shuhao/2018/03/28/Mongo-GridFs文件服务/index.html">
<meta property="og:site_name" content="Niki">
<meta property="og:description" content="1、GridFs文件库使用1.1、添加文件操作GridFs文件库的命令在MONGO_HOMEj/bin 下的mongofiles文件中。我们可以调用该文件中的命令来操作GridFs文件库mongofiles -d gridfs put demo.png 只要往数据库中插入了一个文件，Mongo会为该库新建两个Collection来保存文件相关的内容。这两个Collection的用途我们待会会说道。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-03-28T11:50:27.920Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mongo GridFs文件服务">
<meta name="twitter:description" content="1、GridFs文件库使用1.1、添加文件操作GridFs文件库的命令在MONGO_HOMEj/bin 下的mongofiles文件中。我们可以调用该文件中的命令来操作GridFs文件库mongofiles -d gridfs put demo.png 只要往数据库中插入了一个文件，Mongo会为该库新建两个Collection来保存文件相关的内容。这两个Collection的用途我们待会会说道。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/shuhao/2018/03/28/Mongo-GridFs文件服务/"/>





  <title> Mongo GridFs文件服务 | Niki </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?cb9f0585e406f878cb2f29c89ad3cdb7";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Niki</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">天道酬勤</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-categories"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/shuhao/blog/2018/03/28/Mongo-GridFs文件服务/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Fang ShuHao">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/head.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Niki">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Mongo GridFs文件服务
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-28T17:09:39+08:00">
                2018-03-28
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Mongo/" itemprop="url" rel="index">
                    <span itemprop="name">Mongo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1、GridFs文件库使用"><a href="#1、GridFs文件库使用" class="headerlink" title="1、GridFs文件库使用"></a>1、GridFs文件库使用</h2><h4 id="1-1、添加文件"><a href="#1-1、添加文件" class="headerlink" title="1.1、添加文件"></a>1.1、添加文件</h4><p>操作GridFs文件库的命令在MONGO_HOMEj/bin 下的mongofiles文件中。我们可以调用该文件中的命令来操作GridFs文件库<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">mongofiles -d gridfs put demo.png</span><br></pre></td></tr></table></figure></p>
<p>只要往数据库中插入了一个文件，Mongo会为该库新建两个Collection来保存文件相关的内容。这两个Collection的用途我们待会会说道。</p>
<h4 id="1-2、查找文件"><a href="#1-2、查找文件" class="headerlink" title="1.2、查找文件"></a>1.2、查找文件</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># 查找所有的文件</span><br><span class="line">C:\Users\Administrator&gt;mongofiles -d gridfs list</span><br><span class="line">2018-03-28T17:28:28.282+0800    connected to: localhost</span><br><span class="line">D:/demo.csv     297</span><br></pre></td></tr></table></figure>
<h4 id="1-3、删除文件"><a href="#1-3、删除文件" class="headerlink" title="1.3、删除文件"></a>1.3、删除文件</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">C:\Users\Administrator&gt;mongofiles -d gridfs delete 297</span><br><span class="line">2018-03-28T17:29:41.677+0800    connected to: localhost</span><br><span class="line">successfully deleted all instances of &apos;297&apos; from GridFS</span><br></pre></td></tr></table></figure>
<h4 id="1-4、mongofiles-操作参数"><a href="#1-4、mongofiles-操作参数" class="headerlink" title="1.4、mongofiles 操作参数"></a>1.4、mongofiles 操作参数</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">C:\Users\Administrator&gt;mongofiles --help</span><br><span class="line">Usage:</span><br><span class="line">  mongofiles &lt;options&gt; &lt;command&gt; &lt;filename or _id&gt;</span><br><span class="line"></span><br><span class="line">Manipulate gridfs files using the command line.</span><br><span class="line"></span><br><span class="line">Possible commands include:</span><br><span class="line">        list      - list all files; &apos;filename&apos; is an optional prefix which listed filenames must begin with</span><br><span class="line">        search    - search all files; &apos;filename&apos; is a substring which listed filenames must contain</span><br><span class="line">        put       - add a file with filename &apos;filename&apos;</span><br><span class="line">        get       - get a file with filename &apos;filename&apos;</span><br><span class="line">        get_id    - get a file with the given &apos;_id&apos;</span><br><span class="line">        delete    - delete all files with filename &apos;filename&apos;</span><br><span class="line">        delete_id - delete a file with the given &apos;_id&apos;</span><br></pre></td></tr></table></figure>
<h2 id="2、GridFs文件库简介"><a href="#2、GridFs文件库简介" class="headerlink" title="2、GridFs文件库简介"></a>2、GridFs文件库简介</h2><p> GridFS是基于mongodb存储引擎是实现的“分布式文件系统”，底层基于mongodb存储机制，和其他本地文件系统相比，它具备大数据存储的多个优点。GridFS适合存储超过16MB的大型文件，不过16M数据在当今互联网时代，已经不足为奇。我们可以使用GridFS构建大规模的“图片服务器”、“文档服务器”、“视频、音频”文件服务器，GridFS对于web应用，可以结合nginx插件“ningx-gridfs”能够简单的实现负载均衡等特性，非常便捷</p>
<h4 id="2-1、GridFs文件库存储结构"><a href="#2-1、GridFs文件库存储结构" class="headerlink" title="2.1、GridFs文件库存储结构"></a>2.1、GridFs文件库存储结构</h4><p>GridFS并不是将单个文件直接存储为一个document，而是将文件分成多个parts或者说chunks，然后将每个chunk作为作为一个单独的document存储，然后将chunks有序保存。默认情况下，GridFS的chunk大小位255k。GridFS使用2个collections来存储这些文件，一个collection存储文件的chunks（实际文件数据），另一个则存储文件的metadata（用户自定义的属性，filename，content-type等）。</p>
<p>当用户查询GridFS中的文件时，客户端或者driver将会重新按序组装这些chunks。用户可以range查询文件，也可以获取文件的任意部分的信息，比如：跳过（skip）视频或者音频（任何文件）的中间部，实现“range access of single file”。</p>
<p>对于mongodb而言，每个document最大尺寸为16M，如果想存储一条数据（比如一个文件）超过16M，那么只能使用GridFS支持；GridFS可以支持单个文件尺寸达到数G，读取文件时可以分段读取。此外，GridFS可以从Mongodb的高性能、高可用特性中获益，比如我们可以在“replica set”或者“sharding”架构模式下使用GridFS。</p>
<ol>
<li><p>fs.chunks<br>这个Collection用来保存文件的数据</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;_id&quot; : &lt;ObjectId&gt;,</span><br><span class="line">  &quot;files_id&quot; : &lt;ObjectId&gt;,</span><br><span class="line">  &quot;n&quot; : &lt;num&gt;,</span><br><span class="line">  &quot;data&quot; : &lt;binary&gt;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>fs.files<br>这个Collection用来保存文件的数据</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;_id&quot; : &lt;ObjectId&gt;,</span><br><span class="line">  &quot;length&quot; : &lt;num&gt;,</span><br><span class="line">  &quot;chunkSize&quot; : &lt;num&gt;,</span><br><span class="line">  &quot;uploadDate&quot; : &lt;timestamp&gt;,</span><br><span class="line">  &quot;md5&quot; : &lt;hash&gt;,</span><br><span class="line">  &quot;filename&quot; : &lt;string&gt;,</span><br><span class="line">  &quot;contentType&quot; : &lt;string&gt;,</span><br><span class="line">  &quot;aliases&quot; : &lt;string array&gt;,</span><br><span class="line">  &quot;metadata&quot; : &lt;any&gt;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="3、使用场景"><a href="#3、使用场景" class="headerlink" title="3、使用场景"></a>3、使用场景</h2><p>document的大小超过16M是使用GridFS的条件之一，因为mongodb普通的collection无法支持16M以上的document，我们不得不选择其他方案；在一些情况下，将这些大文件存储在GridFS中，比直接存储在本地文件系统中更加适合：</p>
<ol>
<li><p>如果你的文件系统对每个目录下文件的个数有限制（或者太多，将会影响文件的打开速度等）。</p>
</li>
<li><p>如果你的文件数据，有分数据中心镜像保存（大数据情况，可用性保证）。</p>
</li>
<li><p>如果你希望访问一个超大的文件，而不希望将它全部加入内存，而是有“range access”的情况，即分段读取，那么GridFS天生就具备这种能力，你可以随意访问任意片段。</p>
</li>
</ol>
<p>对于一个大文件，如果你希望原子性的更新它的全部内容，那么GridFS将不适合；比如同时更新一个文件的多个chunk，因为mongodb本身没有事务机制。</p>
<p>对于小于16M的文件，比如一些图片、CSS、js文件等，应该将它们直接存储在普通的collection中而非GridFS（bindata，参见org.bson.types.Binary），因为它们通常较小，GridFS无法发挥其优势。当然，你为了统一application的“文件系统”存储方式，也可以将这些小文件保存在GridFS中，性能也不会差的太多，为了提升性能，对这些小文件，可以在存储时手动设置它的chunkSize，避免文件被切分成多个chunks来提高性能。</p>
<p><a href="https://gitee.com/shuhao/code/tree/master/mongo/gird_fs">java DEMO</a></p>
<blockquote>
<blockquote>
<p>参考<br><a href="https://docs.mongodb.com/manual/reference/program/mongofiles/#bin.mongofiles" target="_blank" rel="external">mongofiles官方介绍</a><br><a href="http://www.runoob.com/mongodb/mongodb-gridfs.html" target="_blank" rel="external">MongoDB GridFS</a><br><a href="http://shift-alt-ctrl.iteye.com/blog/2195646" target="_blank" rel="external">GridFs介绍</a><br><a href="https://blog.csdn.net/w172087242/article/details/51956109" target="_blank" rel="external">Java GridFs操作</a></p>
</blockquote>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Mongo/" rel="tag"># Mongo</a>
          
            <a href="/blog/tags/文件服务/" rel="tag"># 文件服务</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/03/28/Mongo排序sort时内存问题/" rel="next" title="Mongo排序sort时内存问题">
                <i class="fa fa-chevron-left"></i> Mongo排序sort时内存问题
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/03/30/python-解析pdf内容/" rel="prev" title="python 解析pdf内容">
                python 解析pdf内容 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNjY5NS8xMzIzMA=="></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/head.jpg"
               alt="Fang ShuHao" />
          <p class="site-author-name" itemprop="name">Fang ShuHao</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">135</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories">
                <span class="site-state-item-count">35</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">93</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、GridFs文件库使用"><span class="nav-number">1.</span> <span class="nav-text">1、GridFs文件库使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1、添加文件"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.1、添加文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2、查找文件"><span class="nav-number">1.0.2.</span> <span class="nav-text">1.2、查找文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3、删除文件"><span class="nav-number">1.0.3.</span> <span class="nav-text">1.3、删除文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4、mongofiles-操作参数"><span class="nav-number">1.0.4.</span> <span class="nav-text">1.4、mongofiles 操作参数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、GridFs文件库简介"><span class="nav-number">2.</span> <span class="nav-text">2、GridFs文件库简介</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1、GridFs文件库存储结构"><span class="nav-number">2.0.1.</span> <span class="nav-text">2.1、GridFs文件库存储结构</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、使用场景"><span class="nav-number">3.</span> <span class="nav-text">3、使用场景</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Fang ShuHao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  



  
  

  

  

  

  


  

</body>
</html>
